const { ccclass, property } = cc._decorator;
import elevatorstep from '../World/elevatorstep';

@ccclass
export default class HeroCollider extends cc.Component {
    @property
    isSpeedUp: boolean = false;
    @property
    isSpeedDown: boolean = false;
    @property
    SpeedUp: number = 0;
    @property(cc.Node)
    followNode: cc.Node = null;

    body: cc.RigidBody = null;
    speedEd: cc.Vec2 = null;
    _StepControl: elevatorstep = null;
    _isStepStop: boolean = false;
    _helper: HeroHelper = null;
    _heroManeger: HeroManager = null;
    _isChange: boolean = false;
    _barPosition: cc.Vec3 = null;
    onLoad() {
        this._helper = HeroHelper.getInstance();
        this._heroManeger = HeroManager.getInstance();
        this.body = this.node.getComponent(cc.RigidBody);
        this.body.allowSleep = false;
        this.body.linearDamping = 0;
        let Pcollider = this.node.getComponent(cc.PhysicsBoxCollider);
        Pcollider.tag = TagHeroBody;
        let collider = this.node.getComponent(cc.CircleCollider);
        collider.tag = TagHeroBody;
        if (this.followNode)
            this._barPosition = this.followNode.position;
    }
    // 只在两个碰撞体开始接触时被调用一次
    onBeginContact(contact, selfCollider, otherCollider) {
        if (otherCollider.tag == TagWayBody1 || otherCollider.tag == TagWayBody2 || otherCollider.tag == TagStep || otherCollider.tag == TagBox || otherCollider.tag == TagPutBody) {
            this.setFootIn(contact, otherCollider);
            if (otherCollider.tag == TagStep && this._helper.isFootCollider) {
                this._StepControl = otherCollider.node.getComponent(elevatorstep);
            }
            else if (this._StepControl) {
                this.setStepStop(contact, otherCollider);
                this._isStepStop = true;
            }
        }
        else if (otherCollider.tag == TagAbyss) {
            this._helper.isFootCollider = true;
            this._helper.uuidFootCollider = otherCollider.node.uuid;
            let _event = new cc.Event.EventCustom('footBeginContact', true);
            _event.setUserData(otherCollider.tag);
            this.node.dispatchEvent(_event);
        }
        else if (otherCollider.tag == TagMonsterBody) {
            var worldManifold = contact.getWorldManifold();
            var normal = worldManifold.normal;
            if (normal.y < -0.9) {
                this._helper.isFootCollider = true;
                this._helper.uuidFootCollider = otherCollider.node.uuid;
                let _event = new cc.Event.EventCustom('footBeginContact', true);
                _event.setUserData(otherCollider.tag);
                this.node.dispatchEvent(_event);
            }
        }
    }
    setFootIn(contact, otherCollider) {
        var worldManifold = contact.getWorldManifold();
        var normal = worldManifold.normal;
        if (normal.y < -0.9) {
            this._helper.isFootCollider = true;
            this._helper.uuidFootCollider = otherCollider.node.uuid;
            let _event = new cc.Event.EventCustom('footBeginContact', true);
            _event.setUserData(otherCollider.tag)
            this.node.dispatchEvent(_event);
            if (otherCollider.tag == TagWayBody1 || otherCollider.tag == TagWayBody2 ) {
                this._helper.isWayCollider = true;
                if (!this._helper.uuidWayCollider)
                    this._helper.uuidWayCollider = this._helper.uuidFootCollider;
                else if (this._helper.uuidWayCollider != otherCollider.node.uuid) {
                    this._helper.uuidWayCollider = this._helper.uuidFootCollider;
                    this._isChange = true;
                    this.node.dispatchEvent(new cc.Event.EventCustom('footContactChange', true));
                    return;
                }
            }
        }
    }
    setStepStop(contact, otherCollider) {
        var worldManifold = contact.getWorldManifold();
        var normal = worldManifold.normal;
        if (normal.y > 0.8) {
            this._StepControl.onStopStep(true);
        }
    }
    // 只在两个碰撞体结束接触时被调用一次
    onEndContact(contact, selfCollider, otherCollider) {
        if (this._isChange) {
            this._isChange = false;
            return;
        }
        if (otherCollider.node.uuid == this._helper.uuidFootCollider) {
            this._helper.isFootCollider = false;
            this._helper.isWayCollider = false;
            let _event = new cc.Event.EventCustom('footEndContact', true);
            _event.setUserData(otherCollider.tag);
            this.node.dispatchEvent(_event);
        }
        if (otherCollider.tag != TagStep && this._isStepStop && this._StepControl) {
            this._StepControl.onStopStep(false);
            this._StepControl = null;
        }
    }
    update(dt: number) {
        if (this.isSpeedUp) {
            let lv = this.body.linearVelocity;
            if (!this.speedEd) {
                this.speedEd = this.body.linearVelocity.clone();
            }
            if (this._helper.isFootCollider)
                lv.x += (this.SpeedUp * 1.3) * dt;
            else
                lv.x += (this.SpeedUp) * dt;
            this.body.linearVelocity = lv;
        }
        else if (this.isSpeedDown && this.speedEd) {
            if (!this._helper.isFootCollider) {
                let lv = this.body.linearVelocity;
                lv.x -= this.SpeedUp / 2 * dt;
                if (this.speedEd.x >= lv.x) {
                    this.speedEd = null;
                    this.isSpeedDown = false;
                }
                else {
                    this.body.linearVelocity = lv;
                }
            }
        }
        else if (this._heroManeger.isRunState && this._helper.isFootCollider) {
            let lv = this.body.linearVelocity;
            lv.x = this._heroManeger.GetRunSpeed();
            this.body.linearVelocity = lv;
        }
        if (this.followNode) {
            let _position = this._barPosition.add(this.node.position);
            this.followNode.position = _position;
        }
    }
}
